/*
 * Copyright (C) 2014 VenomVendor <info@VenomVendor.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package com.naizhuai.ui.adapter.business;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.naizhuai.R;
import com.naizhuai.ui.entity.SimpleKeyValueBean;
import com.naizhuai.ui.entity.mainbusiness.CheckedTextViewBean;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ActionCheckedEditAdapterHolder extends RecyclerView.Adapter<ActionCheckedEditAdapterHolder.ActionCheckedHolder> {

    private Context mContext;
    private List<CheckedTextViewBean> inputList;
    private SparseArray<SimpleKeyValueBean> choseMap;
    public static final int EDIT_KEY_DATA_INDEX = -1;

    private boolean editEnable=true;


    public ActionCheckedEditAdapterHolder(Context context, List<CheckedTextViewBean> inputList) {
        this.mContext = context;
        this.inputList = inputList;
        choseMap = new SparseArray<>();
    }

    @Override
    public int getItemCount() {
        return inputList.size();
    }

    @NonNull
    @Override
    public ActionCheckedEditAdapterHolder.ActionCheckedHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View sView = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_action_edit_checked, parent, false);
        return new ActionCheckedHolder(sView);
    }

    @Override
    public void onBindViewHolder(@NonNull ActionCheckedHolder holder, int position) {
        CheckedTextViewBean checkedBean = inputList.get(position);
        for (int index = 0; index < holder.describeTxts.length; index++) {
            holder.describeTxts[index].setText(checkedBean.getDescribeText().get(index));
            if(editEnable)
                holder.describeTxts[index].setOnClickListener(new checkDataListener(holder, index, position));
            else
                holder.describeTxts[index].setOnClickListener(null);
        }
        holder.keyTxt.setText(checkedBean.getPointTxt());
        if(editEnable){
            holder.keyTxt.setOnClickListener(new checkDataListener(holder, EDIT_KEY_DATA_INDEX, position));
            holder.editData.setClickable(holder.keyTxt.isSelected());
            holder.editData.setFocusable(holder.keyTxt.isSelected());
        }else{
            holder.keyTxt.setOnClickListener(null);
            holder.editData.setClickable(editEnable);
            holder.editData.setFocusable(editEnable);
        }
        holder.editData.addTextChangedListener(new EditTextChanged(position));
    }

    public boolean isEditEnable() {
        return editEnable;
    }

    public void setEditEnable(boolean editEnable) {
        this.editEnable = editEnable;
    }

    class EditTextChanged implements TextWatcher {
        private int position;

        public EditTextChanged(int position) {
            this.position = position;
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(Editable s) {
            if (choseMap.get(position) == null) {
                choseMap.put(position, new SimpleKeyValueBean(EDIT_KEY_DATA_INDEX, s.toString()));
            } else {
                choseMap.get(position).setKeyValue(EDIT_KEY_DATA_INDEX, s.toString());
            }
        }
    }

    class checkDataListener implements View.OnClickListener {
        private ActionCheckedHolder holder;
        private int choseIndex;
        private int position;

        public checkDataListener(ActionCheckedHolder holder, int choseIndex, int position) {
            this.holder = holder;
            this.choseIndex = choseIndex;
            this.position = position;
        }

        @Override
        public void onClick(View v) {
            if (choseMap.get(position) == null)
                choseMap.put(position, new SimpleKeyValueBean());
            if (v.getId() == holder.keyTxt.getId()) {
                choseMap.get(position).setKeyValue(EDIT_KEY_DATA_INDEX,"");
                selectEdit(true, EDIT_KEY_DATA_INDEX);
            } else {
                choseMap.get(position).setKeyValue(choseIndex,"");
                selectEdit(false, choseIndex);
            }
        }

        private void selectEdit(boolean selectEdit, int choseIndex) {
            holder.keyTxt.setSelected(selectEdit);
            holder.editData.setFocusable(selectEdit);
            holder.editData.setClickable(selectEdit);
            if (selectEdit) {
                for (TextView describe : holder.describeTxts) {
                    describe.setSelected(false);
                }
            } else {
                for (int index = 0; index < holder.describeTxts.length; index++) {
                    if (index != choseIndex)
                        holder.describeTxts[index].setSelected(false);
                    else
                        holder.describeTxts[index].setSelected(true);
                }
            }
        }
    }

    public SparseArray<SimpleKeyValueBean> getPracticeData(){
        return choseMap;
    }

    public class ActionCheckedHolder extends RecyclerView.ViewHolder {
        TextView[] describeTxts;
        TextView keyTxt;
        EditText editData;

        public ActionCheckedHolder(View view) {
            super(view);
            TextView describeTxt = view.findViewById(R.id.data_describe);
            describeTxts = new TextView[]{describeTxt};
            keyTxt = view.findViewById(R.id.data_key);
            editData = view.findViewById(R.id.data_edit);
        }
    }

}
